package com.example.leetcode.baidu;


import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

public class Sample {
    //设置APPID/AK/SK
//    public static final String APP_ID = "24654013";
//    public static final String API_KEY = "LiVGD03LekTlLcDTZx1xwMod";
//    public static final String SECRET_KEY = "ovvEWKk8I6SHTuoQSnAiW8ta1Dn5jdyG";


    public static final String APP_ID = "24655301";
    public static final String API_KEY = "mFjW80xZZEy0ULFS7sf3Fvit";
    public static final String SECRET_KEY = "8ad1CdyFIFGBQmYeFcAGuZY4wmPA7GDY";

    public static void main(String[] args) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String path = "d:/tmp/pic/1.png";
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        parse(res);
        parse(client.basicGeneral("d:/tmp/pic/2.jpg", new HashMap<String, String>()));
        parse(client.basicGeneral("d:/tmp/pic/3.jpg", new HashMap<String, String>()));
//        JSONObject jsonObject = client.webImageUrl("http://n.sinaimg.cn/spider20210801/64/w640h224/20210801/fb46-56f7c72767db745f7db5b4694daf9e38.png", new HashMap<String, String>());
//        System.out.println(jsonObject.toString(2));

    }

    private static List<String> parse(JSONObject res){
        System.out.println(res.toString(2));
        if(!res.has("words_result")){
            return null;
        }
        res.getJSONArray("words_result").forEach(o -> {
            JSONObject jsonObject = (JSONObject) o;
            String words = jsonObject.getString("words");
            if(words.matches("\\d{15,16}||\\w{10}||[A-Za-z0-9]{12}")){
                System.out.println(words.replaceAll("o","0"));
            }
        });
        return null;
    }
}